<template>
  <el-dialog
    v-model="dialogVisible"
    :title="ruleForm.id ? '修改类别' : '新增类别'"
    width="500"
  >
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="100"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="类别名称" prop="typeName">
        <el-input v-model="ruleForm.typeName" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit(ruleFormRef)">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="dialogVisibleImg">
    <img w-full :src="dialogImageUrl" alt="Preview Image" />
  </el-dialog>
</template>

<script setup>
import { ref } from "vue";
import { ElMessageBox } from "element-plus";
import serviceTypeAPI from "@/api/serviceType";

const dialogVisible = ref(false);
const pid = ref();
const init = (value, item) => {
  console.log(item);
  dialogVisible.value = true;
  pid.value = value;
  if (!!item) {
    serviceTypeAPI
      .showServiceInfo(item.id)
      .then((res) => {
        console.log(res);
        for (let key in res) {
          ruleForm[key] = res[key];
        }
      })
      .catch((err) => {});
  }
};
defineExpose({
  init,
});
const emit = defineEmits(["updateTerms"]);
const handleClose = () => {
  // ElMessageBox.confirm("Are you sure to close this dialog?")
  //   .then(() => {})
  //   .catch(() => {});
  dialogVisible.value = false;
  ruleFormRef.value.resetFields();
};

//表单
const ruleForm = reactive({
  typeName: "",
});
const rules = reactive({
  typeName: [
    {
      required: true,
      message: "请输入类别名称",
      trigger: "blur",
    },
  ],
});
const ruleFormRef = ref();
const handleSubmit = async (formEl) => {
  console.log(formEl, "formEl");
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      let params = {
        ...ruleForm,
        pid: pid.value,
        level: 2,
      };
      let request = ruleForm.id
        ? serviceTypeAPI.editServiceType
        : serviceTypeAPI.addServiceType;
      request(params)
        .then((res) => {
          dialogVisible.value = false;
          emit("updateTerms", pid.value);
          ruleFormRef.value.resetFields();
        })
        .catch((err) => {});
    } else {
      console.log("error submit!", fields);
    }
  });
};
//上传图片

const fileList = ref([
  {
    name: "food.jpeg",
    url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
  },
  {
    name: "plant-1.png",
    url: "/images/plant-1.png",
  },
]);

const dialogImageUrl = ref("");
const dialogVisibleImg = ref(false);

const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
};

const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url;
  dialogVisibleImg.value = true;
};
</script>
<style lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
